package com.hxd.otc.module.login.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.RecentLoginAccountInfoHelper;
import com.hxd.otc.module.login.bean.AccountBean;
import com.hxd.otc.module.login.contract.RegisterContract;
import com.hxd.otc.module.my.bean.ImageCodeBean;
import com.hxd.otc.module.my.bean.RegisterRequest;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.EncryptUtils;
import com.hxd.otc.utils.hyphenate.HyphenateKeFuUtil;
import com.hxd.otc.utils.rongcloud.RongCloudUtils;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/4 14:15
 * 描  述 :  ${注册Presenter}
 */
public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements RegisterContract.Presenter {

    private final UserModel mUserModel;

    public RegisterPresenter(RegisterContract.View view) {
        super(view);
        mUserModel = UserModel.getInstance();
    }

    @Override
    public void getVerCode(String mobile, String sessionId,String countryId) {

        mUserModel.sendPhoneCodeWithVerif(new BaseObserver<ImageCodeBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ImageCodeBean imageCodeBean) {
                mView.getVerCodeSuccess(msg);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ImageCodeBean>>bindAutoDispose(), mobile, "register", "Android", "register", sessionId,countryId);
    }

    @Override
    public void getEmailCode(String mail, String sessionId) {
        mUserModel.getEmailCode(new BaseObserver<ImageCodeBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ImageCodeBean imageCodeBean) {
                mView.getVerCodeSuccess(msg);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ImageCodeBean>>bindAutoDispose(), mail, "register", "Android", "register", sessionId);
    }

    @Override
    public void login(final String account, final String password) {
        mUserModel.login(account, EncryptUtils.getRSA(password), null, new BaseObserver<AccountBean>() {
            @Override
            public void onNext(String title, AccountBean resultBean) {
                mView.loginSucceed(resultBean);
                RecentLoginAccountInfoHelper.updateAccountAndPassword(account, EncryptUtils.getRSA(password));
                //登录成功后，重新获取融云token和连接环信客服
                RongCloudUtils.getInstance().requestRongCloudToken();
                HyphenateKeFuUtil.getInstance().register();
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean<AccountBean>>bindAutoDispose());
    }

    @Override
    public void register(RegisterRequest registerRequest) {
        mUserModel.register(new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.registerSucceed(s);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<String>>bindAutoDispose(), registerRequest);
    }

    @Override
    public void findUser(final String account, final int registerMode) {
        mUserModel.findUser(account, new BaseObserver<ImageCodeBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ImageCodeBean imageCodeBean) {
                mView.checkNewPhoneSuccess(account, registerMode);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.checkNewPhoneFail(errMessage);
            }
        }, mView.<BaseBean<ImageCodeBean>>bindAutoDispose());
    }
}
